package com.sky.controller.admin;


import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/report")
@Api("统计接口")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;
    //统计指定日期内的营业额
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnOver(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("统计的营业额时间:{},{}",begin,end);
        TurnoverReportVO turnOver = reportService.getTurnOver(begin, end);
        return Result.success(turnOver);
    }

    //统计指定日期内的用户和新增用户
    @GetMapping("/userStatistics")
    public Result<UserReportVO> userStatistic(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("统计用户量的时间:{},{}",begin,end);
        UserReportVO userReportVO = reportService.getUserStatistics(begin,end);
        return Result.success(userReportVO);
    }
    //指定时间的订单统计
//    @GetMapping("/ordersStatics")
//    public Result<OrderReportVO> ordersStatistic(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//                                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
//        log.info("统计订单");
//    }

    //导出数据
    @GetMapping("/export")
    @ApiOperation("导出数据")
    public void exportBusinessData(HttpServletResponse response) throws IOException {
        log.info("导出近30天数据");
        reportService.exportBusinessData(response);
    }
}
